//
//  NSParserResult.m
//  JSToNativeDemo
//
//  Created by mouxiaochun on 15/7/7.
//  Copyright (c) 2015年 mouxiaochun. All rights reserved.
//

#import "AIResultParser.h"
#import "NSDictionary+Null.h"

//引入各模块的解析头文件
#import "UserModuleApi.h"
#import "CarModuleApi.h"
#import "MainPageModuleApi.h"

static NSString *_errorCode = @"result_code";
static NSString *_errorMsg  = @"message";

@implementation AIRequest (Result)

-(void)handleResponse:(id)responseObject responseData:(NSData *)responseData{
   
    if ([responseObject isKindOfClass:[NSDictionary class]]) {
        NSInteger errorCode      = [responseObject valueForKeyOfInteger:_errorCode];
        NSString *errorMsg  = [responseObject valueForKeyOfNSString: _errorMsg];
        self.result.message = errorMsg; //记录接口信息
        self.result.code    = [NSString stringWithFormat:@"%zi",errorCode];
        if (errorCode == 0) {
            self.result.success = YES;
            
            /**
             **  下面这一段就是需要判断进入到哪一个模块解析
             **/
            
            if ([self.module isEqualToString:_userModuleName]) {

#pragma mark//判断是否是【用户模块】名称
                
                [self userModuleHander:responseObject responseData:responseData];
                
            }else
            if ([self.module isEqualToString:_carModuleName]) {
                
#pragma mark//判断是否是【车辆模块】名称
                
                [self carModuleHander:responseObject responseData:responseData];
                
            }
            else if ([self.module isEqualToString:_mainPageModuleName]){

#pragma mark//判断是否是【首页模块】名称
                [self MainPageModuleHander:responseObject responseData:responseData];

            }else if ([self.module isEqualToString:@""]){
            
            
            }
            
            
        }else{
            
            if (self.result.message.length == 0) {
                self.result.message         = @"请求失败，请重新请求";
            }
            [self excuteBlock];
        }
    }else if([responseObject isKindOfClass:[NSError class]]){
      NSError *error = (NSError *)responseObject;
      self.result.success = NO;
      if (self.result.message.length == 0) {
        if ([error code] == -1001) {
          self.result.message = @"网络连接超时,请稍后重试";
        }else if ([error code] == -1009) {
          self.result.message = @"网络异常,请稍后重试";
        }else{
            self.result.message = @"网络请求失败,请稍后重试!";
        }
      }
      [self excuteBlock];

    }else{
      self.result.success = NO;
      if (self.result.message.length == 0) {
        self.result.message = @"网络异常,请稍后重试";
      }
      [self excuteBlock];
    }
}

@end




